#!/bin/sh

. /lib/functions.sh 

start() {
	config_load sstar
	config_get_bool es audio es 0
	config_get mute audio mute 0
	config_get blacklight video blacklight
	config_get pwm_period video pwm_period
	config_get duty_cycle video duty_cycle

	if [ "$es" -gt 0 ]; then
		insmod mhal.ko IS_MHAL_SUPPORT_ES_CODEC=1
	else
		insmod mhal.ko
	fi

	insmod /lib/modules/4.9.84/mi_common.ko
	insmod /lib/modules/4.9.84/mi_sys.ko cmdQBufSize=128 logBufSize=0
	insmod /lib/modules/4.9.84/mi_gfx.ko
	insmod /lib/modules/4.9.84/mi_divp.ko
	insmod /lib/modules/4.9.84/mi_vdec.ko
	insmod /lib/modules/4.9.84/mi_ai.ko
	insmod /lib/modules/4.9.84/mi_ao.ko
	insmod /lib/modules/4.9.84/mi_disp.ko
	insmod soundcore.ko
	insmod snd.ko
	insmod snd-timer.ko
	insmod snd-pcm.ko
	insmod snd-pcm-oss.ko
	insmod /lib/modules/4.9.84/mi_alsa.ko
	insmod /lib/modules/4.9.84/mi_venc.ko
	insmod /lib/modules/4.9.84/mi_panel.ko

	major=`cat /proc/devices | busybox awk "\\$2==\""mi_poll"\" {print \\$1}"`
	busybox mknod /dev/mi_poll c $major 0
	insmod /lib/modules/4.9.84/fbdev.ko

	major=`cat /proc/devices | busybox awk "\\$2==\""mi_poll"\" {print \\$1}"`
	busybox mknod /dev/mi_poll c $major 0

	# audio mute off
	if [ "$mute" != "0" ]; then
		echo "$mute" >/sys/class/gpio/export
		echo "out" >/sys/class/gpio/gpio$mute/direction
		echo 1 >/sys/class/gpio/gpio$mute/value
	fi

	/usr/sbin/qmsd-pre.sh
	/usr/sbin/qmsd-demo &

	if [ "$blacklight" == "pwm0" ]; then
		# to enable backlight
		echo 0 > /sys/class/pwm/pwmchip0/export
		echo $pwm_period >/sys/class/pwm/pwmchip0/pwm0/period
		echo $duty_cycle >/sys/class/pwm/pwmchip0/pwm0/duty_cycle
		echo 1 >/sys/class/pwm/pwmchip0/pwm0/enable
	fi

	if [ "$blacklight" == "pwm1" ]; then
		echo 0 > /sys/class/pwm/pwmchip0/export
		echo $pwm_period >/sys/class/pwm/pwmchip0/pwm0/period
		echo $duty_cycle >/sys/class/pwm/pwmchip0/pwm0/duty_cycle
		echo 1 >/sys/class/pwm/pwmchip0/pwm0/enable
	fi
}

stop() {
	killall demo
	killall sstar-init

	sleep 2

	rmmod fbdev
	rmmod mi_panel
	rmmod mi_venc
	rmmod mi_ai
	rmmod mi_disp
	rmmod mi_ao
	rmmod mi_vdec
	rmmod mi_divp
	rmmod mi_gfx
	rmmod mi_sys
	rmmod mi_common
	rmmod mhal
}

$1
